﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
using System.Text;
using HKEA.CMS.Core.BLL.App;
using System.Web.SessionState;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// M_GetCPAndBranchOffice 的摘要说明
    /// </summary>
    public class M_GetCPAndBranchOffice : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {

            context.Response.ContentType = "text/plain";
            StringBuilder strBuilder = new StringBuilder();
            string returnStr = string.Empty;
            BasePage basepage = new BasePage();
            tblCustomer customerENT = basepage.GetCustomerFromSession();
            if (customerENT != null)
            {
                string strFormat = "{{\"uid\":\"{0}\",\"type\":\"{1}\",\"name\":\"{2}\",\"adfrom\":\"{3}\"}}";
                app_tblCustomer app_customerENT = customerENT.app_tblCustomer.FirstOrDefault();
                if (app_customerENT != null)
                {
                    strBuilder.Append(string.Format(strFormat, app_customerENT.AppCustomerUID, "cust", app_customerENT.Name_Eng + " " + app_customerENT.Name_Chi, "app"));
                    strBuilder.Append(",");
                }
                else
                {
                    strBuilder.Append(string.Format(strFormat, customerENT.CustomerUID, "cust", customerENT.Name_Eng + " " + customerENT.Name_Chi, "tbl"));
                    strBuilder.Append(",");
                }
                strBuilder.Append(GetCPStr(customerENT, strFormat));
                strBuilder.Append(GetBranchOfficeStr(customerENT, strFormat));
                returnStr = strBuilder.ToString();
                if (!string.IsNullOrEmpty(returnStr))
                {
                    returnStr = returnStr.Substring(0, returnStr.Length - 1);
                }
            }
            else
            { returnStr = ResourceManage.GetResourceString("msg200016"); }
            context.Response.Write(returnStr);
            context.Response.Flush();
        }

        private string GetCPStr(tblCustomer customerENT, string strFormat)
        {
            var app_cust_CPList = customerENT.app_tblCust_ContactPerson.ToList();
            var DeletedCPList = app_cust_CPList.Where(a => a.Deleted == 1).ToList();
            var cust_CPList = customerENT.tblCust_ContactPerson.Where(a => a.Deleted == 0).ToList(); //找出没有被删除的cust下的CP
            StringBuilder strBuilder = new StringBuilder();
            foreach (var cust_cp in cust_CPList)
            {
                if (DeletedCPList.Where(a => a.CustContactUID == cust_cp.CustContactUID).Count() == 0) //CP不存在 member已删除列表时
                {
                    var tempCPENT = cust_cp.tblContactPerson;
                    if (tempCPENT != null)
                    {
                        var appTempCPENT = tempCPENT.app_tblContactPerson.FirstOrDefault();
                        if (appTempCPENT != null)
                            strBuilder.Append(string.Format(strFormat, appTempCPENT.AppContactPersonUID, "cp", appTempCPENT.ChristianName + " " + appTempCPENT.FirstName_Eng + "" + appTempCPENT.LastName_Eng + " " + appTempCPENT.FirstName_Chi + " " + appTempCPENT.LastName_Chi, "app"));
                        else
                            strBuilder.Append(string.Format(strFormat, tempCPENT.ContactPersonUID, "cp", tempCPENT.ChristianName + " " + tempCPENT.FirstName_Eng + "" + tempCPENT.LastName_Eng + " " + tempCPENT.FirstName_Chi + " " + tempCPENT.LastName_Chi, "tbl"));
                        strBuilder.Append(",");
                    }
                }
            }

            if (app_cust_CPList.Count > 0)
            {
                var addCPList = app_cust_CPList.Where(a=>!(a.CustContactUID > 0)); //找出member新增的 CP
                foreach (var addCPENT in addCPList)
                {
                    app_tblContactPerson app_cpENT = new app_tblContactPerson();
                    app_tblContactPersonBLL app_cpBLL = new app_tblContactPersonBLL();
                    app_cpENT.AppContactPersonUID = (int)addCPENT.AppContactPersonUID;
                    app_cpENT = app_cpBLL.GetEntity(app_cpENT);
                    if (app_cpENT != null)
                    {
                        strBuilder.Append(string.Format(strFormat, app_cpENT.AppContactPersonUID, "cp", app_cpENT.ChristianName + " " + app_cpENT.FirstName_Eng + "" + app_cpENT.LastName_Eng + " " + app_cpENT.FirstName_Chi + " " + app_cpENT.LastName_Chi, "app"));
                        strBuilder.Append(",");
                    }
                }
            }
            return strBuilder.ToString();
        }

        private string GetBranchOfficeStr(tblCustomer customerENT,string strFormat)
        {
            var app_branchList = customerENT.app_tblCust_Branch.ToList();
            StringBuilder strBuilder = new StringBuilder();
            tblCust_Branch branchENT = new tblCust_Branch();
            branchENT.Deleted = 0;
            branchENT.CustomerUID = customerENT.CustomerUID;
            tblCust_BranchBLL branchBLL = new tblCust_BranchBLL();
            var branchList = branchBLL.GetListByENT(branchENT); // 找出没有被删除cust下的branch            
            var deleteList = app_branchList.Where(a=>a.Deleted==1);

            foreach (var tempENT in branchList)
            {
                if (deleteList.Where(a => a.CustBranchUID == tempENT.CustBranchUID).Count() == 0) //branch不存在 member已删除列表时
                {
                    var appTempENT = tempENT.app_tblCust_Branch.FirstOrDefault();
                    if(appTempENT!=null)
                        strBuilder.Append(string.Format(strFormat, appTempENT.AppCustBranchUID, "branch", appTempENT.BranchName_Eng + " " + appTempENT.BranchName_Chi, "app"));
                    else
                        strBuilder.Append(string.Format(strFormat, tempENT.CustBranchUID, "branch", tempENT.BranchName_Eng + " " + tempENT.BranchName_Chi, "tbl"));
                    strBuilder.Append(",");
                }
            }
            var addBranchList = app_branchList.Where(a=>!(a.CustBranchUID > 0)).ToList();
            foreach (var addedtempENT in addBranchList)
            {
                strBuilder.Append(string.Format(strFormat, addedtempENT.AppCustBranchUID, "branch", addedtempENT.BranchName_Eng + " " + addedtempENT.BranchName_Chi, "app"));
                strBuilder.Append(",");
            }

            return strBuilder.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}